#!/bin/bash

if [ $# -eq 1 ]; then
    password=$1
else
    echo "请输入密码："
    read -s password
fi
# 提示用户输入密码


# 使用 Expect 执行登录并执行命令
expect <<-EOF
spawn ssh root@121.4.60.15 -p7758 
set timeout -1
expect {
    "password:" {
        send "$password\r"
        exp_continue
    }
    "]#" {
        send "cd /docker/backend \n"
        send "docker load -i ./nest-api-image.tar.gz \n"
        send "docker-compose up -d \n"
        send "sleep 2 \n"
        send "sh ./r.sh \n"
        send "exit \n"
        expect eof
    }
    "Permission denied" {
        puts "密码错误，请检查密码并重新运行脚本"
        exit 1
    }
    timeout {
        puts "连接超时，请检查网络连接并重新运行脚本"
        exit 1
    }
}
expect eof
EOF
